﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using FTchina;
using FTchina.Library;
public partial class UI_login : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        // if (!FTchina.Library.MasterHelper.IsLogin) Response.Redirect("/error.aspx?error=9000");

        if (!IsPostBack)
        {

            basic.CheckUserIP(this);

            litmsg.Text = "<font color=green>" + basic.FilterSQL(Request["title"]) + "</font>";
            RequestHelper.RemoveAllCookies();

        }
    }


    public void Chkuser()
    {

        string name = basic.FilterSQL(txbusername.Text);
        string pass = MD5MK.MD5String(basic.FilterSQL(txbpassword.Text));

        using (DataTable dt = shujuku.GetDataTable("select  m.*,u.id uid  from [master] m,userlist u where m.nicheng='" + name + "' and m.link=u.id"))
        {
            if (dt.Rows.Count == 0)
            {
                LogNet.LogBLL.warn("用户:" + name + " 登陆失败,尝试的密码是:" + basic.FilterSQL(txbpassword.Text));
                Response.Redirect("/error.aspx?error=9001");
            }
            else
            {
                if (dt.Rows[0]["state"].ToString().Trim() == "1")//帐号状态为正常
                {

                    if (pass == dt.Rows[0]["master_password"].ToString().Trim())
                    {
                        MasterHelper.MasterID = MD5MK.MD5String(FTchina.basic.GetGuid());
                        MasterHelper.MasterName = dt.Rows[0]["Master_Name"].ToString().Trim();
                        MasterHelper.MasterPower = dt.Rows[0]["power"].ToString().Trim();
                        MasterHelper.MasterPowerName = dt.Rows[0]["pname"].ToString().Trim();
                        MasterHelper.Nickname = name;
                        MasterHelper.Luruid = dt.Rows[0]["uid"].ToString().Trim();
                        MasterHelper.LoginIP = basic.GetUserIp(this);
                        shujuku.ExecuteSql("update [master] Set cookiess='" + MasterHelper.MasterID + "',[lastime]='" + basic.GetNowDate() + "',[lastip]='" + MasterHelper.LoginIP + "' where nicheng='" + name + "'");
                        LogNet.LogBLL.info("用户:" + name + MasterHelper.LoginIP + "登陆成功");
                        //  ClientScript.RegisterStartupScript ( this.GetType ( ) , "start" , "var win=window.open('../index.aspx','','top=0,left=0,scrollbars=no,status=0,directory=0,location=0');var h = screen.availHeight;var w = screen.availWidth;win.resizeTo(w,h);window.opener=null;window.open('','_self');window.close();" , true );
                        Response.Redirect("../main.aspx");
                    }
                }
                else
                {
                    JS.AlertAndRedirect("您的帐户已被锁定，请与系统管理员联系！", "login.aspx?title=帐户锁定，禁止登录");

                }
            }
        }

    }



    protected void btnok_Click(object sender, EventArgs e)
    {
        //if (txbusername.Text.Length == 0 || txbpassword.Text.Length == 0 || txbcode.Text.Length == 0)

        txbcode.Text = FTchina.Library.RequestHelper.GetCookie("yzmcode");//设置自动获取验证码


        if (txbusername.Text.Length == 0 || txbpassword.Text.Length == 0)
        {
            Response.Redirect("/error.aspx?error=9002");
        }

        if (String.Compare(FTchina.Library.RequestHelper.GetCookie("yzmcode").ToUpper(), txbcode.Text.ToUpper(), true) != 0)
        {
            Response.Redirect("/error.aspx?error=9003");

        }
        Chkuser();
        txbpassword.Text = string.Empty;
        txbusername.Text = string.Empty;

    }

    protected void btnexit_Click(object sender, EventArgs e)
    {

    }
}


